package demo1.Dialogs;

import javax.swing.*;
import java.awt.*;

public class MyDialog extends JDialog {
    private boolean accepted = false;
    private JTextField passWord = new JTextField(10);
    public MyDialog(Window owner) {
        super(owner);

        JPanel root = new JPanel();
        this.setContentPane(root);

        JLabel label = new JLabel("新手教程开始之前");
        root.add(label);

        root.add(passWord);
    }

    public String getPassWord() {
        return passWord.getText();
    }

    public boolean exec() {
        this.setTitle("提示");
        this.setModal( true );
        this.setSize(250, 150);
        centerInOwner();
        // 显示对话框，并阻塞等待
        this.setVisible( true );
        return accepted ; // accepted为true时，表示用户点了确定。为false时表示用户取消了操作。
    }

    private void centerInOwner() {
        Rectangle ownerRect = this.getOwner().getBounds();

        int width = this.getWidth();
        int height = this.getHeight();
        int x = ownerRect.x + (ownerRect.width - width)/2;
        int y = ownerRect.y + (ownerRect.height - height)/2;
        this.setBounds(x,y, width, height);
    }

    private void centerInScreen() {
        // 获取屏幕大小
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        // 显示在屏幕的中央
        int x = ( screenSize.width - this.getWidth())/2;
        int y = ( screenSize.height - this.getHeight())/2;
        this.setLocation(x,  y);
    }
}
